package org.example.bigdata.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.jsonwebtoken.Claims;
import org.example.bigdata.dto.MissionDto;
import org.example.bigdata.entity.Conclusion;
import org.example.bigdata.entity.Growth;
import org.example.bigdata.entity.Mission;
import org.example.bigdata.service.MissionService;
import org.example.bigdata.mapper.MissionMapper;
import org.example.bigdata.utils.JwtUtils;
import org.example.bigdata.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @author Jmx
* @description 针对表【mission】的数据库操作Service实现
* @createDate 2024-05-06 20:13:18
*/
@Service
public class MissionServiceImpl extends ServiceImpl<MissionMapper, Mission>
    implements MissionService{

    @Autowired
    private MissionMapper missionMapper;

    @Override
    public Result fetchMission(String token) {
        Claims claims = JwtUtils.parseJWT(token);
        String phone = claims.get("phone", String.class);
        LambdaQueryWrapper<Mission> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Mission::getPhone,phone);
        List<Mission> list = list(lambdaQueryWrapper);
        return Result.success(list);
    }

    @Override
    public Result submitMission(String token, List<Integer> mission_ids) {
        List<Mission> missions = listByIds(mission_ids);
        for(Mission mission:missions){
            if(mission.getIsFinished()==1){
                return Result.fail("不能重复提交任务！");
            }
        }
        for(Mission mission:missions){
            missionMapper.ChangeFinishedStatus(mission.getMission_id());
        }
        return Result.success();
    }

    @Override
    public Result fetchConclusion(String token) {
        Claims claims = JwtUtils.parseJWT(token);
        String phone = claims.get("phone", String.class);
        Conclusion conclusion = new Conclusion();
        LambdaQueryWrapper<Mission> l = new LambdaQueryWrapper<>();
        l.eq(Mission::getPhone,phone);
        int length = list(l).size();
        LambdaQueryWrapper<Mission> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Mission::getIsFinished,0).eq(Mission::getPhone,phone);
        List<Mission> missions = list(lambdaQueryWrapper);
        List<MissionDto> unfinished = new ArrayList<>();
        for(Mission mission:missions){
            MissionDto missionDto = new MissionDto();
            missionDto.setMission_id(mission.getMission_id());
            missionDto.setContent(mission.getContent());
            unfinished.add(missionDto);
        }
        conclusion.setUnfinished(unfinished);
        // TODO:连续登录天数
        conclusion.setDateCount(1);
        conclusion.setMissionCount(length);
        return Result.success(conclusion);
    }

    @Override
    public Result fetchGrowth(String token) {
        Growth growth = new Growth();
        List<Integer> likes = new ArrayList<>();
        likes.add(1);
        likes.add(2);
        likes.add(3);
        likes.add(4);
        List<Integer> collections = new ArrayList<>();
        collections.add(1);
        collections.add(2);
        collections.add(3);
        collections.add(4);
        List<Integer> comments = new ArrayList<>();
        comments.add(1);
        comments.add(2);
        comments.add(3);
        comments.add(4);
        growth.setLikes(likes);
        growth.setCollections(collections);
        growth.setComments(comments);
        return Result.success(growth);
    }

}




